/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formula.one.urjc.piloto.view;

import formula.one.urjc.Metodos;
import formula.one.urjc.dueño.view.ventanaAviso;
import formula.one.urjc.piloto.CaracteristicasTecnicas;
import formula.one.urjc.piloto.DatosPersonales;
import formula.one.urjc.piloto.Piloto;

/**
 *
 * @author santiago
 */
public final class AltaPiloto extends javax.swing.JInternalFrame {

    //VARIABLES GLOBALES
    //Declaramos este objeto para ejecutar los metodos genericos
    Metodos metodo = new Metodos() ;
    String nombreFichero = "pilotos.dat" ;
    
    /**
     * Creates new form AltaPiloto
     */
    public AltaPiloto() {
        initComponents();
        obtenerValoracion() ;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nombrePiloto = new javax.swing.JTextField();
        apellidosPiloto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        edadPiloto = new javax.swing.JTextField();
        alturaPiloto = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        pesoPiloto = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        crearPiloto = new javax.swing.JButton();
        cancelarAlta = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        valoracionPiloto = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        sueldoPiloto = new javax.swing.JTextField();
        jTextReflejos = new javax.swing.JTextField();
        jTextAgresividad = new javax.swing.JTextField();
        jTextPaciencia = new javax.swing.JTextField();
        jTextValentia = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Alta de Piloto");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Apellidos:");

        jLabel4.setText("Datos Personales:");

        jLabel5.setText("Edad:");

        jLabel6.setText("Altura:");

        jLabel7.setText("Peso:");

        jLabel8.setText("Características Técnicas:");

        jLabel9.setText("Reflejos:");

        jLabel10.setText("Agresividad:");

        jLabel11.setText("Paciencia:");

        jLabel12.setText("Valentía:");

        crearPiloto.setText("Aceptar");
        crearPiloto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearPilotoActionPerformed(evt);
            }
        });

        cancelarAlta.setText("Cancelar");
        cancelarAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarAltaActionPerformed(evt);
            }
        });

        jLabel13.setText("Valoración:");

        valoracionPiloto.setEnabled(false);

        jLabel14.setText("Sueldo:");

        sueldoPiloto.setEnabled(false);

        jTextReflejos.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextReflejosFocusLost(evt);
            }
        });

        jTextAgresividad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextAgresividadFocusLost(evt);
            }
        });

        jTextPaciencia.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextPacienciaFocusLost(evt);
            }
        });

        jTextValentia.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextValentiaFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(alturaPiloto, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                            .addComponent(pesoPiloto)
                            .addComponent(edadPiloto)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nombrePiloto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(apellidosPiloto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addComponent(crearPiloto)
                                .addGap(60, 60, 60)
                                .addComponent(cancelarAlta))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel11))
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextValentia)
                                    .addComponent(jTextPaciencia)
                                    .addComponent(jTextAgresividad)
                                    .addComponent(jTextReflejos, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
                                .addGap(37, 37, 37)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(sueldoPiloto, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGap(8, 8, 8)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel14)
                                            .addComponent(valoracionPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addComponent(jLabel8))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nombrePiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apellidosPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(edadPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(alturaPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(pesoPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jTextReflejos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextAgresividad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextPaciencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextValentia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(valoracionPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sueldoPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(crearPiloto)
                    .addComponent(cancelarAlta))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarAltaActionPerformed
        // Si presionamos Cancelar se cierra la ventana
        this.dispose() ;
    }//GEN-LAST:event_cancelarAltaActionPerformed

    private void crearPilotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearPilotoActionPerformed
        // Declaramos las variables con los valores del formulario
        String nombre_Piloto = nombrePiloto.getText() ;
        String apellidos_Piloto = apellidosPiloto.getText() ;
        try{
            int edad_Piloto = Integer.parseInt(edadPiloto.getText()) ;
            double altura_Piloto = Double.parseDouble(alturaPiloto.getText()) ;
            double peso_Piloto = Double.parseDouble(pesoPiloto.getText()) ;
            double reflejos_Piloto = Double.parseDouble(jTextReflejos.getText()) ;
            double agresividad_Piloto = Double.parseDouble(jTextAgresividad.getText()) ;
            double paciencia_Piloto = Double.parseDouble(jTextPaciencia.getText()) ;
            double valentia_Piloto = Double.parseDouble(jTextValentia.getText()) ;
        //double valoracion_Piloto = Double.parseDouble(valoracionPiloto.getText()) ;
        //double sueldo_Piloto = Double.parseDouble(sueldoPiloto.getText()) ;
        //
            //Inicializamos los valores que no se encuentran en el formulario
            int puntos_Piloto = 0 ;
            
            if(reflejos_Piloto<=5 && reflejos_Piloto>=0 && agresividad_Piloto<=5 && agresividad_Piloto>=0 && paciencia_Piloto<=5 && paciencia_Piloto>=0 && valentia_Piloto<=5 && valentia_Piloto>=0){
            //
            // Declaramos el objeto Datos Personales
            DatosPersonales datos_Personales = new DatosPersonales (edad_Piloto,
                                                                altura_Piloto,
                                                                peso_Piloto) ;
            // Declaramos el objeto Caracteristicas Técnicas
            CaracteristicasTecnicas caracteristicas_Tecnicas = new
                CaracteristicasTecnicas (reflejos_Piloto, agresividad_Piloto,
                                         paciencia_Piloto, valentia_Piloto) ;
        // Declaramos el objeto Piloto
        /*Piloto nuevo_Piloto = new Piloto (nombre_Piloto, apellidos_Piloto,
                                          datos_Personales, puntos_Piloto,
                                          valoracion_Piloto, sueldo_Piloto,
                                          caracteristicas_Tecnicas) ;*/
            Piloto nuevo_Piloto = new Piloto (nombre_Piloto, apellidos_Piloto,
                                          datos_Personales, puntos_Piloto,
                                          caracteristicas_Tecnicas) ;
            //
            // Guardamos el Objeto en el fichero
            metodo.escribirFichero(nombreFichero, nuevo_Piloto) ;
            }else{
                ventanaAviso.mostrarError(this, "Las características técnicas comprenden valores entre 0 y 5", "Error alta piloto");
            }
        }catch(NumberFormatException ne){
            ventanaAviso.mostrarError(this, "Las características técnicas, los datos personales, los puntos y el sueldo deben de ser numéricos", "Error alta piloto");
        }
    }//GEN-LAST:event_crearPilotoActionPerformed

    private void jTextReflejosFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextReflejosFocusLost
         obtenerValoracion() ;
    }//GEN-LAST:event_jTextReflejosFocusLost

    private void jTextAgresividadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAgresividadFocusLost
         obtenerValoracion() ;
    }//GEN-LAST:event_jTextAgresividadFocusLost

    private void jTextPacienciaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextPacienciaFocusLost
          obtenerValoracion() ;
    }//GEN-LAST:event_jTextPacienciaFocusLost

    private void jTextValentiaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextValentiaFocusLost
         obtenerValoracion() ;
    }//GEN-LAST:event_jTextValentiaFocusLost

    public void obtenerValoracion() {
        /*double reflejos_Piloto = Integer.parseInt(reflejosPiloto.getSelectedItem().toString()) ;
        double agresividad_Piloto = Integer.parseInt(agresividadPiloto.getSelectedItem().toString()) ;
        int paciencia_Piloto = Integer.parseInt(pacienciaPiloto.getSelectedItem().toString()) ;
        int valentia_Piloto = Integer.parseInt(valentiaPiloto.getSelectedItem().toString()) ;
        int valoracion_Piloto = reflejos_Piloto + agresividad_Piloto + paciencia_Piloto + valentia_Piloto ;
        valoracionPiloto.setText(Integer.toString(valoracion_Piloto)) ;
        int sueldo_Piloto = valoracion_Piloto * 50 ;
        sueldoPiloto.setText(Integer.toString(sueldo_Piloto)) ;*/
         try{
              double reflejos_Piloto=0;
              double agresividad_Piloto=0;
              double paciencia_Piloto =0;
              double valentia_Piloto=0;
             if(!jTextReflejos.getText().isEmpty()){
                 reflejos_Piloto = Double.parseDouble(jTextReflejos.getText()) ;
             }
             if(!jTextAgresividad.getText().isEmpty()){
                 agresividad_Piloto = Double.parseDouble(jTextAgresividad.getText()) ;
             }  
             if(!jTextPaciencia.getText().isEmpty()){
                paciencia_Piloto = Double.parseDouble(jTextPaciencia.getText()) ; 
             }  
             if(!jTextValentia.getText().isEmpty()){   
                valentia_Piloto = Double.parseDouble(jTextValentia.getText()) ;
             }
             double valoracion_Piloto = reflejos_Piloto + agresividad_Piloto + paciencia_Piloto + valentia_Piloto ;
                valoracionPiloto.setText(String.format("%.1f",valoracion_Piloto)) ;
                double sueldo_Piloto = valoracion_Piloto * 50 ;
                sueldoPiloto.setText(String.format("%.1f",sueldo_Piloto)) ;
            
         }catch(NumberFormatException nE){
             ventanaAviso.mostrarError(this, "Las características técnicas, los datos personales, los puntos y el sueldo deben de ser numéricos", "Error añadir piloto"); 
         }
      
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AltaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AltaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AltaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AltaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AltaPiloto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField alturaPiloto;
    private javax.swing.JTextField apellidosPiloto;
    private javax.swing.JButton cancelarAlta;
    private javax.swing.JButton crearPiloto;
    private javax.swing.JTextField edadPiloto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextAgresividad;
    private javax.swing.JTextField jTextPaciencia;
    private javax.swing.JTextField jTextReflejos;
    private javax.swing.JTextField jTextValentia;
    private javax.swing.JTextField nombrePiloto;
    private javax.swing.JTextField pesoPiloto;
    private javax.swing.JTextField sueldoPiloto;
    private javax.swing.JTextField valoracionPiloto;
    // End of variables declaration//GEN-END:variables
}
